/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import entities.Clientes;
import entities.Logs;
import entities.Usuarios;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Query;
import javax.persistence.RollbackException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import logica.correoElectronico;

/**
 *
 * @author Smuggler
 */
public class AltaClienteReserva extends JPanel {

    private static JFrame frame;
    private Logs logs;
    private Usuarios usuario;
    entities.Clientes ca;
    private boolean desdeReserva = false;

    public AltaClienteReserva(Usuarios usuario, JFrame frame, boolean activoNew) {
        initComponents();
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }
        desdeReserva = activoNew;
        this.frame = frame;
        this.usuario = usuario;
        habilitarBotones(true);
    }

    public JFrame getFrame() {
        return frame;
    }

    public void setFrame(JFrame frame) {
        AltaClienteReserva.frame = frame;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("SIAATPU").createEntityManager();
        query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Clientes c");
        list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
        apellidoLabel = new javax.swing.JLabel();
        nombresLabel = new javax.swing.JLabel();
        procedenciaLabel = new javax.swing.JLabel();
        telefonoLabel = new javax.swing.JLabel();
        celularLabel = new javax.swing.JLabel();
        vehiculoLabel = new javax.swing.JLabel();
        patenteLabel = new javax.swing.JLabel();
        emailLabel = new javax.swing.JLabel();
        extranjeroLabel = new javax.swing.JLabel();
        observacionesLabel = new javax.swing.JLabel();
        apellidoField = new javax.swing.JTextField();
        nombresField = new javax.swing.JTextField();
        procedenciaField = new javax.swing.JTextField();
        telefonoField = new javax.swing.JTextField();
        celularField = new javax.swing.JTextField();
        patenteField = new javax.swing.JTextField();
        emailField = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        extranjeroComboBox = new javax.swing.JComboBox();
        vehiculoComboBox = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        observacionesField = new javax.swing.JTextArea();
        ayudaLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        documentoTextField = new javax.swing.JTextField();

        FormListener formListener = new FormListener();

        apellidoLabel.setText("Apellido:");

        nombresLabel.setText("Nombres:");

        procedenciaLabel.setText("Procedencia:");

        telefonoLabel.setText("Telefono:");

        celularLabel.setText("Celular:");

        vehiculoLabel.setText("Vehiculo:");

        patenteLabel.setText("Patente:");

        emailLabel.setText("Email:");

        extranjeroLabel.setText("Extranjero:");

        observacionesLabel.setText("Observaciones:");

        apellidoField.setEnabled(false);

        nombresField.setEnabled(false);

        procedenciaField.setEnabled(false);

        telefonoField.setEnabled(false);
        telefonoField.addKeyListener(formListener);

        celularField.setEnabled(false);
        celularField.addKeyListener(formListener);

        patenteField.setEnabled(false);

        emailField.setEnabled(false);
        emailField.addFocusListener(formListener);

        saveButton.setBackground(new java.awt.Color(1, 154, 210));
        saveButton.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        saveButton.setForeground(new java.awt.Color(255, 255, 255));
        saveButton.setText("Guardar");
        saveButton.setEnabled(false);
        saveButton.addActionListener(formListener);

        extranjeroComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "NO", "SI" }));
        extranjeroComboBox.setSelectedIndex(-1);
        extranjeroComboBox.setEnabled(false);

        vehiculoComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "NO", "SI" }));
        vehiculoComboBox.setSelectedIndex(-1);
        vehiculoComboBox.setEnabled(false);

        observacionesField.setColumns(20);
        observacionesField.setRows(5);
        observacionesField.setEnabled(false);
        jScrollPane1.setViewportView(observacionesField);

        ayudaLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/help.png"))); // NOI18N
        ayudaLabel.addMouseListener(formListener);

        jLabel1.setText("Documento:");

        documentoTextField.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(apellidoLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(apellidoField, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(nombresLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(nombresField, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel1)
                                .addGap(8, 8, 8)
                                .addComponent(documentoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(celularLabel)
                                    .addComponent(telefonoLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(celularField, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(telefonoField, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(vehiculoLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(extranjeroComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(vehiculoComboBox, 0, 56, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(patenteLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(patenteField, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(ayudaLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(saveButton))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(procedenciaLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(procedenciaField, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(extranjeroLabel)
                                .addGap(209, 209, 209))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(emailLabel)
                                .addGap(39, 39, 39)
                                .addComponent(emailField, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(observacionesLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 459, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(telefonoLabel)
                            .addComponent(telefonoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(celularLabel)
                            .addComponent(celularField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(apellidoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(apellidoLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nombresField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(nombresLabel))
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(vehiculoComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(patenteLabel)
                                .addComponent(patenteField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(vehiculoLabel))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(documentoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(procedenciaLabel)
                            .addComponent(procedenciaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(extranjeroLabel)
                            .addComponent(extranjeroComboBox))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(emailLabel)
                            .addComponent(emailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(46, 46, 46)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(observacionesLabel)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(saveButton)
                        .addGap(0, 19, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(ayudaLabel)))
                .addContainerGap())
        );
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener, java.awt.event.FocusListener, java.awt.event.KeyListener, java.awt.event.MouseListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == saveButton) {
                AltaClienteReserva.this.saveButtonActionPerformed(evt);
            }
        }

        public void focusGained(java.awt.event.FocusEvent evt) {
        }

        public void focusLost(java.awt.event.FocusEvent evt) {
            if (evt.getSource() == emailField) {
                AltaClienteReserva.this.emailFieldFocusLost(evt);
            }
        }

        public void keyPressed(java.awt.event.KeyEvent evt) {
        }

        public void keyReleased(java.awt.event.KeyEvent evt) {
        }

        public void keyTyped(java.awt.event.KeyEvent evt) {
            if (evt.getSource() == celularField) {
                AltaClienteReserva.this.celularFieldKeyTyped(evt);
            }
            else if (evt.getSource() == telefonoField) {
                AltaClienteReserva.this.telefonoFieldKeyTyped(evt);
            }
        }

        public void mouseClicked(java.awt.event.MouseEvent evt) {
        }

        public void mouseEntered(java.awt.event.MouseEvent evt) {
            if (evt.getSource() == ayudaLabel) {
                AltaClienteReserva.this.ayudaLabelMouseEntered(evt);
            }
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
        }

        public void mousePressed(java.awt.event.MouseEvent evt) {
        }

        public void mouseReleased(java.awt.event.MouseEvent evt) {
        }
    }// </editor-fold>//GEN-END:initComponents

    private void habilitarBotones(boolean estado) {
        saveButton.setEnabled(estado);
        apellidoField.setEnabled(estado);
        nombresField.setEnabled(estado);
        procedenciaField.setEnabled(estado);
        telefonoField.setEnabled(estado);
        celularField.setEnabled(estado);
        vehiculoComboBox.setEnabled(estado);
        patenteField.setEnabled(estado);
        emailField.setEnabled(estado);
        extranjeroComboBox.setEnabled(estado);
        observacionesField.setEnabled(estado);
        vehiculoComboBox.setSelectedIndex(-1);
        extranjeroComboBox.setSelectedIndex(-1);
        documentoTextField.setEnabled(estado);
    }

    private Integer getIDCliente(String documento, String procedencia) {
        Query queryIDCliente;
        Clientes cli;
        queryIDCliente = entityManager.createNamedQuery("Clientes.findByDocumentoProcedencia");
        queryIDCliente.setParameter("documento", documento);
        queryIDCliente.setParameter("procedencia", procedencia);
        cli = (Clientes) queryIDCliente.getSingleResult();
        return cli.getIdCliente();
    }

    private boolean existeCliente(String documento) {

        Query queryExisteCliente = entityManager.createNamedQuery("Clientes.findByDocumentoCount");
        queryExisteCliente.setParameter("documento", documento);

        int count = ((Long) queryExisteCliente.getSingleResult()).intValue();
        if ((count > 0)) {
            return true;
        }

        return false;
    }
    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        emailFieldFocusLost(null);

        if (!estadocorreo || "".equals(apellidoField.getText()) || vehiculoComboBox.getSelectedIndex() < 0 || extranjeroComboBox.getSelectedIndex() < 0 || "".equals(documentoTextField.getText())) {

            String guardarmensaje = "";
            if ("".equals(apellidoField.getText())) {
                guardarmensaje = guardarmensaje + "                     Apellido\n";

            }
            if ("".equals(documentoTextField.getText())) {
                guardarmensaje = guardarmensaje + "                     Documento\n";

            }
            if (vehiculoComboBox.getSelectedIndex() < 0) {
                guardarmensaje = guardarmensaje + "                     Vehiculo:\n";

            }
            if (extranjeroComboBox.getSelectedIndex() < 0) {
                guardarmensaje = guardarmensaje + "                     Extranjero:\n";

            }
            if (!estadocorreo) {
                guardarmensaje = guardarmensaje + "                     E-mail:\n";

            }

            JOptionPane.showMessageDialog(this, "Revise el/los siguientes campos:\n" + guardarmensaje, "Atención", JOptionPane.INFORMATION_MESSAGE, null);




        } else {
            if (!existeCliente(documentoTextField.getText())) {
                try {
                    logs = new Logs();
                    logs.setUsuario(usuario);
                    ca = new entities.Clientes();
                    entityManager.persist(ca);
                    list.add(ca);
                    ca.setApellido(apellidoField.getText());
                    ca.setNombres(nombresField.getText());
                    ca.setProcedencia(procedenciaField.getText());
                    ca.setTelefono(telefonoField.getText());
                    ca.setCelular(celularField.getText());
                    ca.setVehiculo(vehiculoComboBox.getSelectedItem().toString());
                    ca.setPatente(patenteField.getText());
                    ca.setEmail(emailField.getText());
                    ca.setExtranjero(extranjeroComboBox.getSelectedItem().toString());
                    ca.setObservaciones(observacionesField.getText());
                    ca.setDocumento(documentoTextField.getText());
                    ca.setActivo("SI");
                    if (ca.getIdCliente() == null) {
                        entityManager.getTransaction().commit();
                        entityManager.getTransaction().begin();
                        Integer idCliente = getIDCliente(ca.getDocumento(), ca.getProcedencia());
                        logs.setearObservaciones("Alta", "Clientes", idCliente, "AltaClienteReserva");
                    }
                    entityManager.persist(logs);
                    entityManager.getTransaction().commit();
                    entityManager.getTransaction().begin();
                } catch (RollbackException rex) {
                    rex.printStackTrace();
                    entityManager.getTransaction().begin();
                    List<entities.Clientes> merged = new ArrayList<entities.Clientes>(list.size());
                    for (entities.Clientes c : list) {
                        merged.add(entityManager.merge(c));
                    }
                    list.clear();
                    list.addAll(merged);
                }
                try {
                    if (desdeReserva) {
                        AltaReservas.setearCliente(ca);
                    } else {
                        VistaNuevaReserva.setearCliente(ca);
                    }
                    this.frame.dispose();    // cerrar la ventana
                } catch (Exception e) {
                    // No cierro la ventana porque el dialogo fue abierto directamente
                }
            } else {
                JOptionPane.showMessageDialog(this, "El Documento ya se encuentra registrado", "Atención", JOptionPane.ERROR_MESSAGE, null);
            }
        }
    }//GEN-LAST:event_saveButtonActionPerformed
    boolean estadocorreo = true;
    private void emailFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_emailFieldFocusLost


        String emailAddress = emailField.getText().toLowerCase();
        emailAddress = emailAddress.replaceAll(" ", "");
        emailField.setText(emailAddress);
        correoElectronico.getcorreoElectronico(emailAddress);


        if (correoElectronico.getcorreoElectronico(emailAddress) || ("".equals(emailField.getText()))) {

            estadocorreo = true;
        } else {
            estadocorreo = false;
        }
    }//GEN-LAST:event_emailFieldFocusLost

    private void celularFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_celularFieldKeyTyped
        char caracter = evt.getKeyChar();

        // Verificar si la tecla pulsada no es un digito
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != '\b' /*
                 * corresponde a BACK_SPACE
                 */)) {
            evt.consume();  // ignorar el evento de teclado
        }       // TODO add your handling code here:
        // TODO add your handling code here:
    }//GEN-LAST:event_celularFieldKeyTyped

    private void ayudaLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ayudaLabelMouseEntered
        ayudaLabel.setToolTipText("En esta pantalla usted podra agregar,\n editar y eliminar unidades de alojamiento");
    }//GEN-LAST:event_ayudaLabelMouseEntered

    private void telefonoFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_telefonoFieldKeyTyped
         char caracter = evt.getKeyChar();

        // Verificar si la tecla pulsada no es un digito
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != '\b' /*
                 * corresponde a BACK_SPACE
                 */)) {
            evt.consume();  // ignorar el evento de teclado
        }       // TODO add your handling code here:
        // TODO add your handling code here:
    }//GEN-LAST:event_telefonoFieldKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField apellidoField;
    private javax.swing.JLabel apellidoLabel;
    private javax.swing.JLabel ayudaLabel;
    private javax.swing.JTextField celularField;
    private javax.swing.JLabel celularLabel;
    private javax.swing.JTextField documentoTextField;
    private javax.swing.JTextField emailField;
    private javax.swing.JLabel emailLabel;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JComboBox extranjeroComboBox;
    private javax.swing.JLabel extranjeroLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private java.util.List<entities.Clientes> list;
    private javax.swing.JTextField nombresField;
    private javax.swing.JLabel nombresLabel;
    private javax.swing.JTextArea observacionesField;
    private javax.swing.JLabel observacionesLabel;
    private javax.swing.JTextField patenteField;
    private javax.swing.JLabel patenteLabel;
    private javax.swing.JTextField procedenciaField;
    private javax.swing.JLabel procedenciaLabel;
    private javax.persistence.Query query;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextField telefonoField;
    private javax.swing.JLabel telefonoLabel;
    private javax.swing.JComboBox vehiculoComboBox;
    private javax.swing.JLabel vehiculoLabel;
    // End of variables declaration//GEN-END:variables

    public void main(Usuarios user, boolean activo) {
        final boolean activoNew = activo;
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AltaClienteReserva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AltaClienteReserva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AltaClienteReserva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AltaClienteReserva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        final Usuarios usuario = user;


        EventQueue.invokeLater(new Runnable() {

            public void run() {
                frame = new JFrame();
                frame.setContentPane(new AltaClienteReserva(usuario, frame, activoNew));
                frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
                frame.setTitle("Reserva   -   Cliente   -   Sistema Integrado de Administración de Alquileres Turísticos  ");
                frame.setIconImage(new ImageIcon(getClass().getResource("/images/logo.png")).getImage());
                frame.addWindowListener(new WindowAdapter() {

                    public void windowClosing(WindowEvent e) {
                        frame.setVisible(false);
                        frame = null;
                    }
                });
            }
        });
    }
}
